package leju.common.widget;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Transformation;

public class PreferentialGallery extends GalleryView{ 
     
	private int max_slop;
	private boolean isScrollingLeft;
	
    public PreferentialGallery(Context context){ 
        super(context);   
        this.setStaticTransformationsEnabled(true); 
        final ViewConfiguration configuration = ViewConfiguration
				.get(getContext());
        initSlop();
    } 
     
    public PreferentialGallery(Context context,AttributeSet attrs){ 
        super(context,attrs); 
        this.setStaticTransformationsEnabled(true); 
        
        initSlop();
    } 
    public PreferentialGallery(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        this.setStaticTransformationsEnabled(true); 
        initSlop();
    } 
     
    private void initSlop(){
    	final ViewConfiguration configuration = ViewConfiguration
				.get(getContext());
    	 max_slop = configuration.getScaledTouchSlop();
    }
    //mCamera����4����3DЧ����,����z�᷽���ϵ�ƽ��,��y�����ת�� 
    private Camera mCamera = new Camera(); 
     
    //mMaxRotationAngle��ͼƬ��y�������ת�Ƕ�,Ҳ������Ļ�������}��ͼƬ����ת�Ƕ� 
    private int mMaxRotationAngle = 60; 
     
    //mMaxZoom��ͼƬ��z��ƽ�Ƶľ���,�Ӿ��Ͽ���4���ǷŴ���С��Ч��. 
    private int mMaxZoom = -120; 
     
    private int mCoveflowCenter; 
    public int getMaxRotationAngle() { 
            return mMaxRotationAngle; 
    } 
    public void setMaxRotationAngle(int maxRotationAngle) { 
            mMaxRotationAngle = maxRotationAngle; 
    } 
    public int getMaxZoom() { 
            return mMaxZoom; 
    } 
    public void setMaxZoom(int maxZoom) { 
            mMaxZoom = maxZoom; 
    } 
    private int getCenterOfCoverflow() { 
            return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 
                            + getPaddingLeft(); 
    } 
    private static int getCenterOfView(View view) { 
            return view.getLeft() + view.getWidth() / 2; 
    } 
    protected boolean getChildStaticTransformation(View child, Transformation t) { 
            final int childCenter = getCenterOfView(child); 
            final int childWidth = child.getWidth(); 
            int rotationAngle = 0; 
            t.clear(); 
            t.setTransformationType(Transformation.TYPE_MATRIX); 
            if (childCenter == mCoveflowCenter) { 
                    transformImageBitmap(child, t, 0); 
            } else { 
                    rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); 
                    if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                            rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle 
                                            : mMaxRotationAngle; 
                    }
                    transformImageBitmap( child, t, rotationAngle); 
            } 
            return true; 
    } 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
            mCoveflowCenter = getCenterOfCoverflow(); 
            super.onSizeChanged(w, h, oldw, oldh); 
    } 
    private void transformImageBitmap(View child, Transformation t, 
                    int rotationAngle) { 
            mCamera.save(); 
            final Matrix imageMatrix = t.getMatrix(); 
            final int imageHeight = child.getLayoutParams().height; 
            final int imageWidth = child.getLayoutParams().width; 
            final int rotation = Math.abs(rotationAngle); 
            // ��Z���������ƶ�camera���ӽǣ�ʵ��Ч��Ϊ�Ŵ�ͼƬ�� 
            // �����Y�����ƶ�����ͼƬ�����ƶ���X���϶�ӦͼƬ�����ƶ��� 
            mCamera.translate(0.0f, 0.0f, 120f); 
            // As the angle of the view gets less, zoom in 
            if (rotation < mMaxRotationAngle) {
                    float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); 
                    mCamera.translate(0.0f, -rotation*2/3, zoomAmount); 
            }
            // ��Y������ת����ӦͼƬ�������﷭ת�� 
            // �����X������ת�����ӦͼƬ�������﷭ת�� 
            //mCamera.rotateY(rotationAngle); 
            mCamera.getMatrix(imageMatrix); 
            imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); 
            imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); 
            mCamera.restore(); 
    }
    int lastX;
    boolean isScrolling;
	
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		switch(action){
		case MotionEvent.ACTION_DOWN:
			lastX = (int)ev.getX();
			isScrolling = false;
			break;
		case MotionEvent.ACTION_MOVE:
			int curX = (int)ev.getX();
			if(Math.abs(curX - lastX) >= max_slop){
				isScrolling = true;
			}else{
				isScrolling = false;
			}
			touchEventLastX = curX;
			break;
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			touchEventLastX = ev.getAction();
			isScrolling = false;
			break;
		}
		return isScrolling;
	}

	int touchEventLastX;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
		switch(action){
		case MotionEvent.ACTION_DOWN:
			touchEventLastX = (int)event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			int curX = (int)event.getX();
			if(curX >= touchEventLastX){
				isScrollingLeft = true;
			}else{
				isScrollingLeft = false;
			}
			break;
		case MotionEvent.ACTION_UP:
			break;
		}
		return super.onTouchEvent(event);
	}

	@Override
	protected boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return isScrollingLeft;
	} 
    
} 
